package com.lanou.bookstore.cart.web.servlet;


import com.lanou.bookstore.cart.domain.CartItem;
import com.lanou.bookstore.cart.service.CartService;
import com.lanou.bookstore.cart.service.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/cart")
public class CartServlet extends HttpServlet {

    CartService cs = new CartServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        String clear = req.getParameter("clear");
        String bid = req.getParameter("bid");

        String uname = (String) session.getAttribute("uname");
//        String cartName = uname.concat("cartList");


        if (session != null && session.getAttribute("uname") != null) {


            if ("clearOne".equals(clear)) {
                //删除一本

                List<CartItem> cartList = (List<CartItem>) req.getSession().getAttribute("cartList");
                List<CartItem> itemList = cs.delCartListOne(bid, cartList);

                req.getSession().setAttribute("cartList", itemList);

            } else if ("clearAll".equals(clear)) {
                //删除所有

                List<CartItem> cartList = (List<CartItem>) req.getSession().getAttribute("cartList");
                List<CartItem> itemList = cs.delCartListAll(cartList);

                req.getSession().setAttribute("cartList", itemList);

            } else if ("add".equals(clear)) {//if ("add".equals(clear))
                //添加到购物车

                String count = req.getParameter("count");
                List<CartItem> cartList = (List<CartItem>) req.getSession().getAttribute("cartList");

                boolean a = true;
                for (CartItem cartItem : cartList) {
                    if (cartItem.getBook().getBid().equals(bid)) {
                        cartItem.setNum(cartItem.getNum() + Integer.parseInt(count));
                        a = false;
                    }
                }
                if (a) {
                    cartList = cs.addCartList(count, bid, cartList);
                }
                req.getSession().setAttribute("cartList", cartList);

            }
//            req.getSession().setAttribute("cartName", cartName);
            req.getRequestDispatcher("/jsps/cart/list.jsp").forward(req, resp);

        } else {
            req.getSession().setAttribute("relogin", "islogined");
            resp.sendRedirect("/jsps/user/login.jsp");
//            req.getRequestDispatcher("/jsps/user/login.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
}
